const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
const communityservice = require("../../../apiservice/community/communityservice");
let userinfo = wx.getStorageSync('useridentity');
// pages/community/add/add.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		userinfo: [],
		street: {
			new_streetid: '',
			new_name: '请选择'
		},
		position: {},
		currentname: '',
		currentcontact: '',
		currentmobile: '',
		provinceid:'',
		cityid:'',
		countyid:''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		userinfo = wx.getStorageSync('useridentity');
		this.setData({
			userinfo: userinfo,
			position: {
				provinceid: options.provinceid,
				cityid: options.cityid,
				countyid: options.countyid,
			},
			provinceid: options.provinceid,
			cityid: options.cityid,
			countyid: options.countyid,
			currentcontact: '',
			currentmobile: '',
			currentname: ''
		})
		//console.log(userinfo);
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		//console.log(this.data.street);
		//this.getPCANameById();
	},

// 	getPCANameById: async function() {
// 		//debugger;
// 		let provinceid = this.data.provinceid;
// 		let cityid = this.data.cityid;
// 		let areaid = this.data.countyid;
// 		if (provinceid && cityid && areaid) {
// 			let result = await communityservice.getPCANameById(provinceid, cityid, areaid);
// 			if (result.data) {
// 				let data = result.data;
// 				this.setData({
// 					["position.provincename"]: data[0] ? data[0] : '',
// 					["position.cityname"]: data[1] ? data[1] : '',
// 					["position.areaname"]: data[2] ? data[2] : ''
// 				})
// 			}
// 
// 		}
// 
// 	},

	toStreetlist: function() {
		//debugger
		//console.log("跳转到街道详细列表");
		let toUrl = "../../street/list/list?type=choose";
		if (this.data.position.provinceid &&
			this.data.position.cityid &&
			this.data.position.countyid) {
            toUrl+=`&province=${this.data.position.provinceid}`
			       +`&city=${this.data.position.cityid}`
				   +`&area=${this.data.position.countyid}`;
		}
		wx.navigateTo({
			url: toUrl,
		});
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},



	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},
	onChange: function(e) {
		var target = e.currentTarget.dataset.id;
		if (target == 'contactname') {
			this.setData({
				currentcontact: e.detail
			})
		}
		if (target == 'mobile') {
			this.setData({
				currentmobile: e.detail
			})
		}
		if (target == 'name') {
			this.setData({
				currentname: e.detail
			})
		}
	},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	addCommunity: function() {
		//debugger;
		var position = this.data.position;
		var communityobj = {
			new_province: position.provinceid,
			new_city: position.cityid,
			new_county: position.countyid,
			userid: this.data.userinfo.userid,
			new_name: this.data.currentname,
			new_mobile: this.data.currentmobile,
			new_street: this.data.street.new_streetid,
			contactname: this.data.currentcontact
		};
		// console.log(communityobj);
		var msg = '';
		var isCreate = true;
		var flag = true;
		if (communityobj.new_name.replace(/\s+/g, '') == '' && flag == true) {
			msg = '请输入社区名称';
			isCreate = false;
			flag = false;
		}
		if (communityobj.new_street == '' && flag == true) {
			msg = '请选择街道';
			isCreate = false;
			flag = false;
		}
		if (isCreate == false) {
			wx.showToast({
				title: msg,
				icon: 'none',
				duration: 2000
			})
		} else {
			this.addCommunityById(communityobj);
		}


	},
	//添加社区信息
	addCommunityById: function(obj) {
		var that = this;
		communityservice.addCommunityById(obj).then(function(success) {
			that.showResult(success.data);
			console.log(success);
			wx.navigateBack({
				url: '../list/list'
			})
		}).catch(function(ex) {
			console.log(ex);
		})

	},
	showResult: function(res) {
		var title = '';
		var icon = '';
		if (res.resultState == 1) {
			title = '创建成功';
			icon = 'success';
		} else {
			title = '创建失败';
			icon = 'none';
		}
		wx.showToast({
			title: title,
			icon: icon,
			duration: 2000
		});
	},
})
